﻿KISSY.add("fgt/map/detailMap", function (S) {
    function detailMap(option) {
        var self = this;
        self._init(option);
    }
    S.augment(detailMap, {
        _init: function (option) {
            var self = this,
                $ = S.all;

            self.containerId = option.containerId;
            self.level = option.mapLevel;
            self.lat = option.lat;
            self.lng = option.lng;
        },

        _bindMarkerEvents: function () {
            var self = this;
        },

        Show: function () {
            var self = this;

            if (typeof self.g_Map === "undefined") {
                var ll = new google.maps.LatLng(self.lat, self.lng);
                self.g_Map = new google.maps.Map(document.getElementById(self.containerId), { mapTypeId: google.maps.MapTypeId.ROADMAP, scrollwheel: true, center: ll, zoom: 10 });
                if (typeof self.level == "number") {
                    self.g_Map.setZoom(self.level);
                }
                self.g_Marker = new google.maps.Marker({ position: ll, map: self.g_Map, visible: true, icon: CONFIG["imgCDNServer"] + "static/img/map/IAMHere.png" });
                self._bindMarkerEvents();
            }
        },

        SetPosition: function (lat, lng) {
            var self = this;
            self.lat = lat;
            self.lng = lng;
            var ll = new google.maps.LatLng(self.lat, self.lng);
            self.g_Marker.setPosition(ll);
            self.g_Map.setCenter(ll);
        }
    });

    return detailMap;
});KISSY.add("fgt/map/labelMap", function (S) {
    function labelMap(option) {
        var self = this;
        self.lableMarkerWindowInfoInit = "<div class='labelMap'><p class='promText'>我是可以移动的呦!</p></div>";
        self.lableMarkerWindowInfoConfirm = "<div class='labelMap'><div class='promText'><p>拖动到此位置吗？</p><p>还可以继续拖动的呦！</p></div><div class='links clearfix'><a href='javascript:;' class='button1 cfm-save' id='btn_map_ok'>确定</a><a href='javascript:;' class='button6 cfm-cancel' id='btn_map_cancel'>取消</a></div></div>";
        self.g_dragStarted = 0;
        self._init(option);
    }

    S.augment(labelMap, {
        _init: function (option) {
            var self = this,
                $ = S.all;

            self.option = option;
            self.containerId = option.containerId;
            self.latInput = $("#" + option.latInputId);
            self.lngInput = $("#" + option.lngInputId);
            self.okBtn = $("#btn_map_ok");
            self.cancelBtn = $("#btn_map_cancel");
            self.level = option.mapLevel;
            self.lat = option.lat;
            self.lng = option.lng;
            self.onConfirmLocationChanged = option.onConfirmLocationChanged;

            self.defaultInfoWindow = new google.maps.InfoWindow({
                content: self.lableMarkerWindowInfoInit
            });
            self.defaultInfoWindow.isOpened = false;
            self.dragInfoWindow = new google.maps.InfoWindow({
                content: self.lableMarkerWindowInfoConfirm
            });
            self.dragInfoWindow.isOpened = false;
        },

        _bindMarkerEvents: function () {
            var self = this;
            google.maps.event.addListener(self.defaultInfoWindow, "closeclick", function () {
                self.CloseBothInfoWindow();
            });

            google.maps.event.addListener(self.dragInfoWindow, "closeclick", function () {
                self.CloseBothInfoWindow();
            });

            google.maps.event.addListener(self.dragInfoWindow, "domready", function () {
                S.all("#btn_map_ok").on("click", function () {
                    var ll = self.g_Marker.getPosition();
                    self.lat = ll.lat();
                    self.lng = ll.lng();

                    if (typeof self.latInput != "undefined" && typeof self.lngInput != "undefined") {
                        self.latInput.val(self.lat)
                        self.lngInput.val(self.lng)
                    }

                    if (typeof self.onConfirmLocationChanged == "function") {
                        self.onConfirmLocationChanged();
                    }
                    self.CloseBothInfoWindow();
                });

                S.all("#btn_map_cancel").on("click", function () {
                    self.CloseBothInfoWindow();
                });
            });

            google.maps.event.addListener(self.g_Marker, "click", function () {
                if (self.defaultInfoWindow.isOpened === true || self.dragInfoWindow.isOpened === true) return;

                self.SwitchToDefaultInfoWindow();
            });

            google.maps.event.addListener(self.g_Marker, "dragstart", function () {
                self.CloseBothInfoWindow();
            });

            google.maps.event.addListener(self.g_Marker, "dragend", function () {
                self.SwitchToDragInfoWindow();
            });
        },

        Show: function () {
            var self = this;
            if (typeof self.g_Map === "undefined") {
                self.g_Map = new google.maps.Map(document.getElementById(self.containerId),
                                                    { mapTypeId: google.maps.MapTypeId.ROADMAP,
                                                        scrollwheel: true,
                                                        zoom: 10,
                                                        center: new google.maps.LatLng(39, 116)
                                                    });

                if (typeof self.level === "number") {
                    self.g_Map.setZoom(self.level);
                }
            }

            if (typeof self.g_Marker === "undefined") {
                self.g_Marker = new google.maps.Marker({ map: self.g_Map, visible: true });
                self.g_Marker.setDraggable(true);
            }

            if (typeof self.lat === "number" && typeof self.lng === "number") {
                var ll = new google.maps.LatLng(self.lat, self.lng);
                self.g_Marker.setPosition(ll);
                self.g_Map.setCenter(ll);
            }
            self._bindMarkerEvents();
            self.SwitchToDefaultInfoWindow();
        },

        GetMap: function () {
            var self = this;
            return self.g_Map;
        },
        GetMarker: function () {
            var self = this;
            return self.g_Marker;
        },

        ResetMarker: function (ll) {
            var self = this;
            self.g_Marker.setPosition(ll);
            self.g_Map.setCenter(ll);
            self.SwitchToDefaultInfoWindow();
        },

        SwitchToDefaultInfoWindow: function () {
            var self = this;
            self.dragInfoWindow.close();
            self.dragInfoWindow.isOpened = false;
            self.defaultInfoWindow.open(self.g_Map, self.g_Marker);
            self.defaultInfoWindow.isOpened = true;
        },

        SwitchToDragInfoWindow: function () {
            var self = this;
            self.defaultInfoWindow.close();
            self.defaultInfoWindow.isOpened = false;
            self.dragInfoWindow.open(self.g_Map, self.g_Marker);
            self.dragInfoWindow.isOpened = true;
        },

        CloseBothInfoWindow: function () {
            var self = this;
            self.defaultInfoWindow.close();
            self.defaultInfoWindow.isOpened = false;
            self.dragInfoWindow.close();
            self.dragInfoWindow.isOpened = false;
        }
    });

    return labelMap;
}, { requires: [] });
KISSY.add("fgt/map", function (S, labelMap, detailMap) {
    var map = {
        getLabelMap: function (option) {
            return new labelMap(option);
        },
        getDetailMap: function (option) {
            return new detailMap(option);
        }
    };
    S.FGT.map = map;

    return map;
}, { requires: ["./map/labelMap", "./map/detailMap"] });